/**
 *Copyright [2010] [lcj(caijunlu@gmail.com)]
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License. 
 *You may obtain a copy of the License at 
 *             http://www.apache.org/licenses/LICENSE-2.0 
 *Unless required by applicable law or agreed to in writing, 
 *software distributed under the License is distributed on an "AS IS" BASIS, 
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 *either express or implied. See the License for the specific language governing permissions and limitations under the License
 */
package org.independent.servlet.session.interceptor;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

import org.independent.servlet.session.manager.ResponseCookie;
import org.independent.servlet.session.service.ContextHolderFactory;

/**
 * intercept the method of httpServlet's service put the request and response to
 * current thread
 * 
 * @author lcj
 * 
 */
public aspect ServletServiceInterceptor {
	pointcut service(javax.servlet.Servlet httpServlet, ServletRequest req,
			ServletResponse res):(call(public void javax.servlet.Servlet.service(javax.servlet.ServletRequest,javax.servlet.ServletResponse))||execution(public void javax.servlet.Servlet.service(javax.servlet.ServletRequest,javax.servlet.ServletResponse)))&&target(httpServlet)&&args(req,res);

	void around(javax.servlet.Servlet httpServlet, ServletRequest req,
			ServletResponse resp)  :service(httpServlet, req,resp){
		proceed(httpServlet, req, resp);
		if (ContextHolderFactory.getServiceContextHolder().getContext()
				.getSessionIdCommitType() == 0
				&& resp instanceof HttpServletResponse
				&& resp != null
				&& ContextHolderFactory.getServiceContextHolder().getContext()
						.getSession() != null) {
			ResponseCookie.handleResponse((HttpServletResponse) resp,
					ContextHolderFactory.getServiceContextHolder().getContext()
							.getSession().getId());
		}
	}
}
